<template>
    <div>
    <el-form inline class="demo-form-inline">
    <el-row>
        <el-col :span="5">
            <el-form-item label="所属分类">
                <CatalogCascader :catalogId="query.payload.catalogId" @selectCatalogId="selectCatalogId"/>
            </el-form-item>
        </el-col>
        <el-col :span="5">
            <el-form-item label="品牌">
                <el-select v-model="query.payload.brandId" placeholder="请选择">
                    <el-option v-for="item in brands" :label="item.name" :value=item.brandId />
                </el-select>
            </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="价格区间">
                <el-input-number v-model="query.payload.minPrice" :min="0*1" controls-position="right" />
                &nbsp;-&nbsp;
                <el-input-number v-model="query.payload.maxPrice" :min="0*1" controls-position="right" />
            </el-form-item>
        </el-col>
    </el-row>
        <el-form-item>
            <el-button type="primary" activity @click="queryPmsSkuInfo">查询</el-button>
            <el-button type="warning" activity @click="reset">重置</el-button>
        </el-form-item>
    </el-form>
    <el-divider style="margin: 12px 0"/>
    <el-table ref="table" border v-loading="loading" :data="data.tableData" header-cell-class-name="headerClass">
        <el-table-column align="center" type="index"  label="序号" width="80px" />
        <el-table-column align="center" prop="skuName" label="名称" width="400px" show-overflow-tooltip/>
        <el-table-column align="center" prop="skuDesc" label="描述" width="80px"/>
        <el-table-column align="center" prop="catalogName" width="100px" label="所属分类" />
        <el-table-column align="center" prop="brandName" width="100px" label="品牌" />
        <el-table-column align="center" prop="skuDefaultImg" label="默认图片" width="80px">
            <template v-cloak #default="scope">
                <el-image style="width: 50px; height: 50px"
                          :preview-src-list="[scope.row.skuDefaultImg]"
                          :src="scope.row.skuDefaultImg" fit="fill" :preview-teleported="true" />
            </template>
        </el-table-column>
        <el-table-column align="center" prop="skuTitle" label="标题"  show-overflow-tooltip/>
        <el-table-column align="center" prop="skuSubtitle" label="副标题"  show-overflow-tooltip/>
        <el-table-column align="center" prop="price" width="100px" label="价格" />
        <el-table-column align="center" prop="saleCount" width="60px" label="销量" />
        <!--<el-table-column align="center"  prop="updateTime" width="170px" label="更新时间" >-->
            <!--<template #default="scope">-->
                <!--moment(scope.row.updateTime).format("yyyy-MM-DD HH:mm:ss")}}-->
            <!--</template>-->
        <!--</el-table-column>-->
        <el-table-column align="center" label="操作" width="150px" >
            <template #default="scope">
                <el-link title="查看" type="primary" @click="editPmsSkuInfo(scope.row.skuId)">
                    <el-icon :size="20">
                        <Edit />
                    </el-icon>
                    查看
                </el-link>
                &nbsp;
                <el-popconfirm title="删除？" @confirm="deletePmsSkuInfo(scope.row.skuId)"  @cancel="">
                    <template #reference>
                        <el-link type="danger" title="删除">
                            <el-icon :size="20">
                                <Delete />
                            </el-icon>
                            删除
                        </el-link>
                    </template>
                </el-popconfirm>
            </template>
        </el-table-column>
</el-table>
<el-pagination
        style="display: flex;align-items:center;justify-content:center;"
        v-model:currentPage="query.currentPage"
        v-model:page-size="query.pageSize"
        :page-sizes="[10, 20, 30]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="query.total"
        @size-change="queryPmsSkuInfo"
        @current-change="queryPmsSkuInfo"
/>
<ModPmsSkuInfo @close="modPmsSkuInfo.isOpen = false"  @reload="queryPmsSkuInfo"
              ref="modPmsSkuInfo"
              :openType="transData.openType"
              :id="transData.id" />
</div>
</template>

<script setup>
    import ModPmsSkuInfo from '@/views/mall/pms/components/ModPmsSkuInfo.vue'
    import request from '@/utils/request.js'
    import { ElMessage } from 'element-plus'
    import {ref, reactive, onMounted} from 'vue'
    import moment from 'moment'
    let transData = reactive({})
    let modPmsSkuInfo = ref(null)
    let brands = reactive([])
    let data = reactive({
        modRole:false,
        tableData:[],
        openType:"",
        id:"",
    });
    let loading = ref(true)
    let query = reactive({dateRange:[],payload:{}, currentPage:1,pageSize:10,total:0})
    onMounted(() => {
        queryPmsSkuInfo()
    })
    function queryPmsSkuInfo(){
        query.startTime = query.dateRange[0]
        query.endTime = query.dateRange[1]
        request.post("/api/pmsSkuInfo/getPmsSkuInfoPage", query).then(resp => {
            data.tableData = resp.records
            query.total = resp.total
            loading.value = false
        })
    }

    function reset(){
            query.payload.skuName = ""
            query.payload.skuDesc = ""
            query.payload.catalogId = ""
            query.payload.brandId = ""
            query.payload.skuDefaultImg = ""
            query.payload.skuTitle = ""
            query.payload.skuSubtitle = ""
            query.payload.minPrice = null
            query.payload.maxPrice = null
            query.payload.saleCount = ""
    }

    function deletePmsSkuInfo(id){
        request.get(`/api/pmsSkuInfo/delete/${id}`).then(resp => {
            queryPmsSkuInfo()
            ElMessage({message: '删除成功！',openType: 'success'})
        });
    }

    function editPmsSkuInfo(id){
        transData.id = id
        transData.openType = "edit"
        modPmsSkuInfo.value.isOpen = true
    }

    function selectCatalogId(catId) {
        query.payload.catalogId=catId
        query.payload.brandId = null
        request.get(`/api/pmsBrand/getBrandsByCatalogId/${catId}`).then(resp => {
            brands.length=0
            brands.push(...resp)
        })
    }
</script>
<style lang="css">

</style>


